package org.jeecg.modules.online.cgform.converter;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.MyClassLoader;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.converter.impl.*;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class ConvertFactory {
    /**下拉*/
    private static final String LIST = "list";
    /**单选*/
    private static final String RADIO = "radio";
    /**多选*/
    private static final String CHECKBOX = "checkbox";
    /**下拉多选*/
    private static final String LIST_MULTI = "list_multi";
    /**下拉搜索框*/
    private static final String SEARCH = "sel_search";
    /**自定义树控件*/
    private static final String SEL_TREE = "sel_tree";
    /**分类字典树*/
    private static final String CAT_TREE = "cat_tree";
    /**联动组件*/
    private static final String LINK_DOWN = "link_down";
    /**部门组件*/
    private static final String DEP = "sel_depart";
    /**用户组件*/
    private static final String USER = "sel_user";
    /**省市区组件*/
    private static final String PCA = "pca";

    public static FieldCommentConverter create(OnlCgformField field){
        String showType = field.getFieldShowType();
        FieldCommentConverter obj = null;
        switch (showType) {
            case LIST: case RADIO:
                obj = new DictEasyConverter(field);
                break;
            case LIST_MULTI: case CHECKBOX:
                obj = new MultiSelectConverter(field);
                break;
            case SEARCH:
                obj = new DictTableConverter(field);
                break;
            case SEL_TREE:
                obj = new TreeSelectConverter(field);
                break;
            case CAT_TREE:
                obj = new CatTreeConverter(field);
                break;
            case LINK_DOWN:
                obj = new LinkDownConverter(field);
                break;
            case DEP:
                obj = new DepartConverter(field);
                break;
            case USER:
                obj = new UserSelectConverter(field);
                break;
            case PCA:
                obj = new PcaConverter(field);
                break;
            default:
                obj = null;
        }
        return obj;
    }

    public static Map<String,FieldCommentConverter> getAllConverterByFieldConfig(List<OnlCgformField> fieldList){
        Map<String,FieldCommentConverter> map = new HashMap<String,FieldCommentConverter>();
        for(OnlCgformField field:fieldList){
            FieldCommentConverter converter = null;
            if(oConvertUtils.isNotEmpty(field.getConverter())){
                converter = getConverterByString(field.getConverter().trim());
            }else{
                converter = create(field);
            }
            if(converter==null){
                continue;
            }
            map.put(field.getDbFieldName(),converter);
        }
        return map;
    }

    /**
     * 根据配置的字符串 实例化FieldCommentConverter
     * 可以配置类路径/配置java bean
     * @param converter
     * @return
     */
    private static FieldCommentConverter getConverterByString(String converter){
        Object obj = null;
        if(converter.indexOf(".")>0){
            try {
                obj = MyClassLoader.getClassByScn(converter).newInstance();
            } catch (InstantiationException e) {
                log.error(e.getMessage(), e);
            } catch (IllegalAccessException e) {
                log.error(e.getMessage(), e);
            }
        }else{
            obj = SpringContextUtils.getBean(converter);
        }
        if(obj!=null && obj instanceof FieldCommentConverter) {
            FieldCommentConverter bean = (FieldCommentConverter) obj;
            return bean;
        }
        return null;
    }
}
